package com.drinks.yiniang.io.api;

import com.alibaba.fastjson.JSON;
import com.drinks.yiniang.entity.common.StringJson;
import com.drinks.yiniang.entity.user.LoginJson;
import com.drinks.yiniang.io.http.HttpUrls;
import com.hansen.library.Constants;
import com.hansen.library.entity.BaseJson;
import com.hansen.library.io.tuple.Tuple2;
import com.hansen.library.listener.APIRequestCallback;
import com.hansen.library.utils.LogUtils;
import com.hansen.library.utils.MD5Util;

/**
 * 登录api
 * Created by han on 2019/3/6 0006.
 */

public class LoginApiIO extends BaseApiIO {
    volatile static LoginApiIO instance;

    static public LoginApiIO getInstance() {
        if (instance == null) {
            synchronized (LoginApiIO.class) {
                if (instance == null) {
                    instance = new LoginApiIO();
                }
            }
        }
        return instance;
    }

    /**
     * 用户登录
     *
     * @param mobile   登录手机号
     * @param phoneCode 验证码
     * @param callback
     */
    public void doUserLogin(String mobile, String phoneCode, final APIRequestCallback<LoginJson, Tuple2<Integer, String>> callback) {
        paramsMap.clear();

        paramsMap.put("mobile", mobile);
        paramsMap.put("phone_code", phoneCode);
        paramsMap.put("terminal", TYPE_LOGIN_TERMINAL);
        paramsMap.put("type", TYPE_LOGIN_CLIENT);

        MainApiIO.getInstance().postRequest(HttpUrls.API_LOGIN, paramsMap, new APIRequestCallback<String, Tuple2<Integer, String>>() {

            @Override
            public void onSuccess(String res) {
                LogUtils.d("用户登录===onSuccess");

                LoginJson mDataJson = JSON.parseObject(res, LoginJson.class);

                if (callback != null) {
                    if (mDataJson != null && mDataJson.getData() != null && mDataJson.getData().getUser() != null) {
                        callback.onSuccess(mDataJson);
                    } else {
                        callback.onError(new Tuple2<>(RESULT_ERROR_CODE, RESULT_ERROR_MSG));
                    }
                }
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                if (callback != null) {
                    callback.onError(error);
                }
            }

            @Override
            public void onComplete() {
                if (callback != null) {
                    callback.onComplete();
                }
            }
        });
    }

    /**
     * 微信授权登录
     *
     * @param code   微信code
     * @param callback
     */
    public void doWxAuthLogin(String code, final APIRequestCallback<LoginJson, Tuple2<Integer, String>> callback) {
        paramsMap.clear();
        paramsMap.put("code", code);

        MainApiIO.getInstance().postRequest(HttpUrls.API_LOGIN_WEIXIN, paramsMap, new APIRequestCallback<String, Tuple2<Integer, String>>() {

            @Override
            public void onSuccess(String res) {
                LogUtils.d("微信授权登录===onSuccess");

                LoginJson mDataJson = JSON.parseObject(res, LoginJson.class);

                if (callback != null) {
                    if (mDataJson != null && mDataJson.getData() != null && mDataJson.getData().getUser() != null) {
                        callback.onSuccess(mDataJson);
                    } else {
                        callback.onError(new Tuple2<>(RESULT_ERROR_CODE, RESULT_ERROR_MSG));
                    }
                }
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                if (callback != null) {
                    callback.onError(error);
                }
            }

            @Override
            public void onComplete() {
                if (callback != null) {
                    callback.onComplete();
                }
            }
        });
    }

    /**
     * 绑定手机号
     *
     * @param mobile   手机号
     * @param phoneCode 验证码
     * @param callback
     */
    public void doBindPhone(String mobile, String phoneCode, final APIRequestCallback<LoginJson, Tuple2<Integer, String>> callback) {
        paramsMap.clear();
        paramsMap.put("mobile", mobile);
        paramsMap.put("phone_code", phoneCode);
        paramsMap.put("terminal", TYPE_LOGIN_TERMINAL);

        MainApiIO.getInstance().postRequest(HttpUrls.API_BIND_PHONE, paramsMap, new APIRequestCallback<String, Tuple2<Integer, String>>() {

            @Override
            public void onSuccess(String res) {
                LogUtils.d("绑定手机号===onSuccess");

                LoginJson mDataJson = JSON.parseObject(res, LoginJson.class);

                if (callback != null) {
                    if (mDataJson != null && mDataJson.getData() != null && mDataJson.getData().getUser() != null) {
                        callback.onSuccess(mDataJson);
                    } else {
                        callback.onError(new Tuple2<>(RESULT_ERROR_CODE, RESULT_ERROR_MSG));
                    }
                }
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                if (callback != null) {
                    callback.onError(error);
                }
            }

            @Override
            public void onComplete() {
                if (callback != null) {
                    callback.onComplete();
                }
            }
        });
    }

    /**
     * 退出登录
     *
     * @param callback
     */
    public void doLogout(final APIRequestCallback<StringJson, Tuple2<Integer, String>> callback) {
        paramsMap.clear();

        MainApiIO.getInstance().postRequest(HttpUrls.API_LOGOUT, paramsMap, new APIRequestCallback<String, Tuple2<Integer, String>>() {

            @Override
            public void onSuccess(String res) {
                LogUtils.d("退出登录===onSuccess");

                StringJson mDataJson = JSON.parseObject(res, StringJson.class);

                if (callback != null) {
                    if (mDataJson != null) {
                        callback.onSuccess(mDataJson);
                    } else {
                        callback.onError(new Tuple2<>(RESULT_ERROR_CODE, RESULT_ERROR_MSG));
                    }
                }
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                if (callback != null) {
                    callback.onError(error);
                }
            }

            @Override
            public void onComplete() {
                if (callback != null) {
                    callback.onComplete();
                }
            }
        });
    }
}
